package com.xueyi.system.auxiliary.controller;

import com.xueyi.system.auxiliary.domain.query.AuxiliaryQuery;
import com.xueyi.system.auxiliary.domain.dto.AuxiliaryDto;
import com.xueyi.system.auxiliary.service.IAuxiliaryService;
import com.xueyi.system.material.domain.query.MaterialQuery;
import com.xueyi.system.material.domain.dto.MaterialDto;
import com.xueyi.system.material.service.IMaterialService;
import com.xueyi.common.web.entity.controller.SubBaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xueyi.common.security.annotation.RequiresPermissions;
import com.xueyi.common.core.web.validate.V_A;
import com.xueyi.common.core.web.validate.V_E;
import com.xueyi.common.core.web.result.AjaxResult;
import com.xueyi.common.log.annotation.Log;
import com.xueyi.common.log.enums.BusinessType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

/**
 * 投入品信息管理 业务处理
 *
 * @author yanhe
 */
@RestController
@RequestMapping("/auxiliary")
public class AuxiliaryController extends SubBaseController<AuxiliaryQuery, AuxiliaryDto, IAuxiliaryService, MaterialQuery, MaterialDto, IMaterialService> {

    @Autowired
    private IMaterialService materialService;

    @Autowired
    private IAuxiliaryService iAuxiliaryService;



    /** 定义节点名称 */
    @Override
    protected String getNodeName() {
        return "投入品信息" ;
    }

    /** 定义子数据名称 */
    @Override
    protected String getSubName() {
        return "投入品库" ;
    }

    /**
     * 查询投入品信息列表
     */
    @Override
    @GetMapping("/list")
    @RequiresPermissions(Auth.AUXILIARY_LIST)
    public AjaxResult list(AuxiliaryQuery auxiliary) {
        return super.list(auxiliary);
    }

    /**
     * 查询投入品信息详细
     */
    @Override
    @GetMapping(value = "/{id}")
    @RequiresPermissions(Auth.AUXILIARY_SINGLE)
    public AjaxResult getInfoExtra(@PathVariable Serializable id) {
        return super.getInfoExtra(id);
    }

    /**
     * 投入品信息导出
     */
    @Override
    @PostMapping("/export")
    @RequiresPermissions(Auth.AUXILIARY_EXPORT)
    @Log(title = "投入品信息管理", businessType = BusinessType.EXPORT)
    public void export(HttpServletResponse response, AuxiliaryQuery auxiliary) {
        super.export(response, auxiliary);
    }

    /**
     * 投入品信息新增
     */
    @Override
    @PostMapping
    @RequiresPermissions(Auth.AUXILIARY_ADD)
    @Log(title = "投入品信息管理", businessType = BusinessType.INSERT)
    public AjaxResult add(@Validated({V_A.class}) @RequestBody AuxiliaryDto auxiliary) {

        MaterialDto materialDto = materialService.selectById(auxiliary.getMaterialId());
        BigDecimal add = materialDto.getQuantity().add(auxiliary.getAuxiliaryNum());
        materialDto.setQuantity(add);
        materialService.update(materialDto);
        return super.add(auxiliary);
    }

    /**
     * 投入品信息修改
     */
    @Override
    @PutMapping
    @RequiresPermissions(Auth.AUXILIARY_EDIT)
    @Log(title = "投入品信息管理", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@Validated({V_E.class}) @RequestBody AuxiliaryDto auxiliary) {
        MaterialDto materialDto = materialService.selectById(auxiliary.getMaterialId());
        AuxiliaryDto auxiliaryDto = iAuxiliaryService.selectById(auxiliary.getId());
        BigDecimal auxiliaryNum = auxiliaryDto.getAuxiliaryNum();
        BigDecimal subtract = auxiliary.getAuxiliaryNum().subtract(auxiliaryNum);
        BigDecimal add = materialDto.getQuantity().add(subtract);
        materialDto.setQuantity(add);
        materialService.update(materialDto);
        return super.edit(auxiliary);
    }

    /**
     * 投入品信息批量删除
     */
    @Override
    @DeleteMapping("/batch/{idList}")
    @RequiresPermissions(Auth.AUXILIARY_DEL)
    @Log(title = "投入品信息管理", businessType = BusinessType.DELETE)
    public AjaxResult batchRemove(@PathVariable List<Long> idList) {
        return super.batchRemove(idList);
    }

    /**
     * 获取投入品信息选择框列表
     */
    @Override
    @GetMapping("/option")
    public AjaxResult option() {
        return super.option();
    }

    interface Auth {
        /** 系统 - 投入品信息管理 - 列表 */
        String AUXILIARY_LIST = "system:auxiliary:list" ;
        /** 系统 - 投入品信息管理 - 详情 */
        String AUXILIARY_SINGLE = "system:auxiliary:single" ;
        /** 系统 - 投入品信息管理 - 新增 */
        String AUXILIARY_ADD = "system:auxiliary:add" ;
        /** 系统 - 投入品信息管理 - 修改 */
        String AUXILIARY_EDIT = "system:auxiliary:edit" ;
        /** 系统 - 投入品信息管理 - 删除 */
        String AUXILIARY_DEL = "system:auxiliary:delete" ;
        /** 系统 - 投入品信息管理 - 导出 */
        String AUXILIARY_EXPORT = "system:auxiliary:export" ;
    }
}
